package com.kaigejava.directexchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * Created by kaigejava on 2019/7/11.
 * direct 路由生产者
 */
public class Producer4DirectExchange {
    public static void main(String[] args) throws  Exception {
        //1:创建一个connectioFactory工厂对象，并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置ip 端口 vhost等
        connectionFactory.setHost("192.168.31.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        //2：通过工厂对象获取到connection对象
        Connection connection = connectionFactory.newConnection();

        //3：通
        // 过connection对象获取到一个消息通信的通道 channel
        Channel channel = connection.createChannel();
        //4 声明 路由名称 routingkey
        String exchangeName = "test_direct_exchange";
        String  routingKey = "test.direct.key";
        //5:发送
        String msg = "this is a msg from direct exchange......";
        channel.basicPublish(exchangeName,routingKey,null,msg.getBytes());

        System.out.println("======生产者发送消息完成=====");
    }
}
